package com.yswl.pri.picture;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yswl.pri.R;
import com.yswl.pri.common.ToolbarMFragment;
import com.yswl.pri.eventbus.PictureDialogEvent;
import com.yswl.pri.picture.adapter.ImagsListAdapterLoftDetail;
import com.yswl.pri.picture.entry.ImageBean;
import com.yswl.pri.picture.entry.ImageLoftBean;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import yswl.com.klibrary.base.BaseRecyclerAdapter;
import yswl.com.klibrary.http.CallBack.HttpCallback;
import yswl.com.klibrary.http.HttpClientProxy2;
import yswl.com.klibrary.util.L;

public class PictureLoftPageListFragment extends ToolbarMFragment implements HttpCallback<String> {
    public static final String TAG = PictureLoftPageListFragment.class.getSimpleName();
    private static final int LOAD_MORE = 1;
    private static final int REFRESH = 2;
    private static final String ARG_PARAM1 = "param1";

    private ImageLoftBean mParam1;
    private PictureDialog mPictureDialog;

    public static PictureLoftPageListFragment newInstance(ImageLoftBean param1) {
        PictureLoftPageListFragment fragment = new PictureLoftPageListFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = (ImageLoftBean) getArguments().getSerializable(ARG_PARAM1);
        }
    }

    ImagsListAdapterLoftDetail mAdapter;
    RefreshLayout refreshLayout;
    RecyclerView recyclerView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_pic_list, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        //initUI
        refreshLayout = view.findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                loadData(REFRESH);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                loadData(LOAD_MORE);
            }
        });
         recyclerView = view.findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        SpacesItemDecoration decoration = new SpacesItemDecoration(5);
        recyclerView.addItemDecoration(decoration);
        mAdapter = new ImagsListAdapterLoftDetail(getActivity(), null, R.layout.adapter_image_list_item);
        mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(final int position) {
                //进行列表

                if (mPictureDialog == null){
                    mPictureDialog = PictureDialog.getInstance();
                }
                mPictureDialog.show(getChildFragmentManager(), "loft");
                PictureDialogEvent.postEvent(defultImagBean, position);
            }
        });
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);
        refreshLayout.autoRefresh();
        refreshLayout.setEnableLoadMore(false);
    }

    private void loadData(int Mode) {
        if (mParam1 == null) return;
        if (mParam1.link.startsWith("http"))
            HttpClientProxy2.getInstance().getAsyn(mParam1.link, Mode, null, this);
    }

    List<ImageBean> defultImagBean = new ArrayList<>();

    List<ImageBean> copyImageData() {
        List<String> baiduImageBeans = mAdapter.getList();
        List<ImageBean> imageBeans = new ArrayList<>();
        if (baiduImageBeans != null) {
            for (int i = 0; i < baiduImageBeans.size(); i++) {
                String currBai = baiduImageBeans.get(i);
                ImageBean currIma = new ImageBean();
                currIma.image_url = currBai;
                imageBeans.add(currIma);
            }
        }
        return imageBeans;
    }

    @Override
    public void onSucceed(int requestId, String result) {
        L.d(TAG, " onSucceed : " + result);
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if (TextUtils.isEmpty(result)) return;

        Document document = Jsoup.parse(result);
        Elements elements = document.select(".img a");
        if (elements == null || elements.size() == 0) {
            return;
        }
        List<String> imgs = new ArrayList<>(elements.size());
        for (int i = 0; i < elements.size(); i++) {
            String src = elements.get(i).attr("bigimgsrc");
//            String src = elements.get(i).attr("src");
            imgs.add(src);
        }
        if (requestId == REFRESH) {
            mAdapter.setList(imgs);
        } else {
            mAdapter.addList(imgs);
        }
        defultImagBean = copyImageData();
    }

    @Override
    public void onFail(int requestId, String errorMsg) {
        L.e(TAG, " onFail : " + errorMsg);
        refreshLayout.finishRefresh(false);
        refreshLayout.finishLoadMore(100, false, false);//传入false表示加载失败
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mAdapter = null;
        recyclerView =null;
    }
}
